package com.dcdata;

import java.io.*;

/**
 * @author: xinmi
 * @date: 2020/11/18
 * @description:
 */
public class FileMerge {
    public static void main(String[] args){

        String filen1 = "C:\\Users\\xinmi\\Desktop\\tmp\\1.txt";
        String filen2 = "C:\\Users\\xinmi\\Desktop\\tmp\\2.txt";

        String filenm = "C:\\Users\\xinmi\\Desktop\\cur\\m.txt";;

        File file1 = new File(filen1);
        FileInputStream fin1 = null;

        try {

            System.out.println("filenm");

            BufferedReader br1 = new BufferedReader(new FileReader(filen1));
            BufferedReader br2 = new BufferedReader(new FileReader(filen2));
            String line1 = null;
            String line2 = null;
            while( (line1 = br1.readLine()) != null && (line2 = br2.readLine()) != null ){
                writeLocal(filenm, line1 + " " + line2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static synchronized void writeLocal(String fileName , String response) {
        System.out.println("writeLocal filenm " + response);
        File file = new File(fileName);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file, true);
            byte[] bytes = (response + "\n").getBytes();
            for (byte aByte : bytes) {
                fos.write(aByte);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                assert fos != null;
                fos.flush(); //强制刷新输出流
                fos.close(); //强制关闭输出流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
